package org.springcloud.wisdom.spring_01_canteen.indent;

import org.apache.ibatis.annotations.*;
import org.springcloud.wisdom.spring_01_canteen.login.entity.Mall;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Mapper
public interface IndentDao {
    // 插入新订单
    @Insert("INSERT INTO wisdom.indent (indentid, useid, addressid, mallid, totalprice, orderdate, paystatus, signstatus,name,address,phone,mname,payid,paytime,gettime) " +
            "VALUES (#{indentid}, #{useid}, #{addressid}, #{mallid}, #{totalprice}, #{orderdate}, '待支付', ' ',#{name},#{address},#{phone},#{mname},#{payid},#{paytime},#{gettime})")
    int insertIndent(Indent indent);
    // 查询所有订单
   /* @Select("SELECT * FROM wisdom.indent")
    List<Indent> selectAllIndents();*/
    //动态查询
    @Select("<script>" +
            "SELECT * FROM wisdom.indent" +
            "<where>" +
            "    <if test=\"mallid != null\">" +
            "       mallid = #{mallid}"+
            "    </if>" +
            "</where>" +
            " order by case "+
            " when signstatus = '未接单' then 0 else 1 "+
            " end,indentid desc"+
            "</script>")
    List<Indent> selectAllIndents(Integer mallid);

    @Select("<script>" +
            "SELECT * FROM wisdom.indent" +
            "<where>" +
            "    <if test=\"indentid != null\">" +
            "       indentid = #{indentid}"+
            "    </if>" +
            "</where>" +
            "</script>")
    Indent selectAllIndents1(String indentid);
    //订单状态
    @Update("<script>"
            + "update wisdom.indent"
            + "    <set>"+
            "            <if test=\"null!= signstatus\">" +
            "                signstatus =#{signstatus}," +
            "            </if>"+
            "            <if test=\"null!= payid\">" +
            "                payid =#{payid}," +
            "            </if>"+
            "            <if test=\"null!= paytime\">" +
            "                paytime =#{paytime}," +
            "            </if>"+
            "            <if test=\"null!= paystatus\">" +
            "                paystatus =#{paystatus}" +
            "            </if>"+
            "    </set>"
            + "    where indentid = #{indentid}"
            + "</script>")
    public int updatesign(Indent indent);

    // 计算订单总数
    @Select("SELECT COUNT(*) FROM wisdom.indent")
    int countIndents();

    // 计算特定日期的订单数量
    @Select("SELECT COUNT(*) FROM wisdom.indent WHERE DATE_FORMAT(orderdate, 'yyyy-MM-dd') = DATE_FORMAT(#{date}, 'yyyy-MM-dd') AND mallid = #{mallid}")
    int countIndentsByDateAndMid(@Param("date") Date date, @Param("mallid") Integer mallid);

    //商家总营业额
    @Select("SELECT SUM(totalprice) FROM wisdom.indent WHERE mallid = #{mallid}")
    BigDecimal sumTotalPriceByMallId(@Param("mallid") Integer mallid);
}
